package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempOrderDetailDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempOrderDetail;
import com.sc.nft.entity.vo.OrderDetailListVO;
import com.sc.nft.entity.vo.OrderSimpleVO;
import com.sc.nft.enums.*;
import com.sc.nft.mapper.TempOrderDetailMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.OrderService;
import com.sc.nft.service.TempOrderDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-08-02
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempOrderDetailServiceImpl implements TempOrderDetailService {


    private final TempOrderDetailDao tempOrderDetailDao;

    private final OrderService orderService;

    private final TempOrderDetailMapper tempOrderDetailMapper;

    private final ExportRecordsDao exportRecordsDao;

    @Override
    public Boolean saveTempOrderDetail(Integer type, Long productId, Date payTimeFrom, Date payTimeTo) {
        Long exportRecordsId = getExportRecordsId(type);

        try {
            //首发订单导出
            if (type.equals(OrderExportTypeEnum.ORDER.getCode())) {
                List<OrderSimpleVO> orderSimpleVOList = orderService.orderExport(productId);
                List<TempOrderDetail> tempOrderDetailList = BeanUtil.copyToList(orderSimpleVOList, TempOrderDetail.class);
                tempOrderDetailList.forEach(t -> t.setExportRecordsId(exportRecordsId));
                tempOrderDetailMapper.saveBatch(tempOrderDetailList);
            } else {
                //订单明细导出
                List<OrderDetailListVO> orderDetailListVOS = orderService.orderDetailExport(productId, payTimeFrom, payTimeTo);
                List<TempOrderDetail> tempOrderDetailList = BeanUtil.copyToList(orderDetailListVOS, TempOrderDetail.class);
                tempOrderDetailList.forEach(t -> t.setExportRecordsId(exportRecordsId));
                tempOrderDetailMapper.saveBatch(tempOrderDetailList);
            }

        } catch (Exception e) {
            //导出失败
            log.error("订单or订单明细导出失败:", e);

            return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
        }

        return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
    }


    private static Long getExportRecordsId(Integer marketType) {

        StringBuilder exportName = new StringBuilder();
        int exportType = 0;
        //首发订单
        if (MarketTypeEnum.BRAND_INCUBATION == MarketTypeEnum.getMarketTypeEnum(marketType)) {
            exportName.append(ExportTypeEnum.TEMP_ORDER.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_ORDER.getCode();
            //首发订单明细
        } else {
            exportName.append(ExportTypeEnum.TEMP_ORDER_DETAIL.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_ORDER_DETAIL.getCode();
        }
        //增加导出记录
        ExportRecords exportRecords = new ExportRecords();
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(exportType);
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        return exportRecordsId;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempOrderDetail> findTempOrderDetailList(Long exportRecordsId) {
        return tempOrderDetailDao.getTempOrderDetailList(exportRecordsId);
    }


}
